package com.scalar.chiptrack.adaptor.ikanos.ase;

import com.scalar.chiptrack.jobs.JobHandler;
import com.scalar.chiptrack.adaptor.AdaptorConstants;
import com.scalar.chiptrack.adaptor.AdaptorUtils;
import com.scalar.chiptrack.adaptor.AssemblyLot;
import com.scalar.chiptrack.adaptor.InventoryLot;
import com.scalar.chiptrack.adaptor.ShippedLot;
import com.scalar.chiptrack.adaptor.dao.AssemblyAdaptorDAO;
import com.scalar.chiptrack.adaptor.dao.TestAdaptorDAO;
import com.scalar.chiptrack.adaptor.dao.InventoryLotDAO;
import com.scalar.chiptrack.adaptor.dao.FabSortAdaptorDAO;
import com.scalar.chiptrack.adaptor.dao.ShippedLotDAO;
import com.scalar.chiptrack.adaptor.dao.AdaptorDAO;
import com.scalar.chiptrack.adaptor.dao.AdaptorRunInfo;
import com.scalar.chiptrack.adaptor.dao.NewAdaptorDAO;
import com.scalar.chiptrack.utils.Logger;
import com.scalar.chiptrack.utils.StringUtils;
import com.scalar.chiptrack.utils.DateUtil;
import com.scalar.ScalarException;

import java.util.HashMap;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Date;
import java.io.File;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

import jxl.Workbook;
import jxl.Sheet;
import jxl.Cell;
import jxl.CellType;

/**
 * User: ChandraSekhar
 * Date: Aug 26, 2008
 * Time: 6:49:50 PM
 */
public class ASEKDataParser extends JobHandler implements AdaptorConstants
{
    private HashMap<String, InventoryLot> m_dieBankDataMap    = new HashMap<String, InventoryLot>();
    private HashMap<String, AssemblyLot> m_assemblyDataMap    = new HashMap<String, AssemblyLot>();
    private HashMap<String, ShippedLot> m_assyShipDataMap     = new HashMap<String, ShippedLot>();

    private Logger m_parserLogger   = null;
    private String m_dataOutputDir  = null;

    public ASEKDataParser() throws ScalarException
    {
        try
        {
            m_dataOutputDir = AdaptorUtils.getDataOutputDir( VENDOR_NAME_ASEK, LOCATION_TYPE_ASSEMBLY);
            AdaptorUtils.makeAllAdaptorDirs( m_dataOutputDir );
            m_parserLogger = AdaptorUtils.getDataParserLogger( m_dataOutputDir,VENDOR_NAME_ASEK );
            AssemblyAdaptorDAO.setLogger( m_parserLogger );
            TestAdaptorDAO.setLogger( m_parserLogger );
            InventoryLotDAO.setLogger(m_parserLogger);
            FabSortAdaptorDAO.setLogger(m_parserLogger);
            ShippedLotDAO.setLogger( m_parserLogger );
            AdaptorUtils.setLogger( m_parserLogger );
        }
        catch (ScalarException se)
        {
            log("ERROR!! ERROR!! ERROR!! Exception while loading ASEK WIP output directory details from database, Exception: " + se);
            throw se;
        }
    }
    /**
     *
     * @param args
     * @throws Exception
     */
    public static void main( String args[] ) throws Exception
    {
        try
        {
            new ASEKDataParser().execute();
        }
        catch (Exception e)
        {
            throw e;
        }
    }
    /**
     *
     * @throws Exception
     */
    public void execute() throws Exception
    {
        AdaptorUtils.printHeader(VENDOR_NAME_ASEK, CONSTANT_PARSER);

        File doneDestDir = new File(  m_dataOutputDir + "/done" );

        File sourceDataoutDir = new File( m_dataOutputDir );

        File[] fileList = sourceDataoutDir.listFiles();
        if ( ( fileList == null ) || ( fileList.length <= 3 ) )
        {
            log( "No file found in directory: " + sourceDataoutDir );
            throw new ScalarException( "\n+-----------No file found in directory " + sourceDataoutDir );
        }

        ArrayList<File> successFileList = new ArrayList<File>();

        boolean success = false;

        for ( int i = 0; i < fileList.length; i++ )
        {
            if ( fileList[i].isDirectory() == true )
            {
                continue;
            }

            try
            {
                // Parse Files to get Object List
                if ( fileList[i].getName() != null && fileList[i].getName().trim().startsWith( ASEKPOP3Adaptor.FILE_NAME_ASEK_WIP ) )
                {
                    // To parse the the ASEK_WIP_STATUS sheet in to table
                    ArrayList aseclWipList = parseASEKWipReport( fileList[i].getAbsolutePath() );

                    if ( aseclWipList != null && aseclWipList.size() > 0 )
                    {
                        //to get the ASEK Wip Details details
                        getASEKAssyWipDetails( aseclWipList );
                    }

                    successFileList.add( fileList[i] );
                }

                if ( fileList[i].getName() != null && fileList[i].getName().trim().startsWith( ASEKPOP3Adaptor.FILE_NAME_SHIP ) )
                {
                    // To parse the the ASEK SHIP_STATUS sheet
                    ArrayList asekShipList = parseASEKShipReport( fileList[i].getAbsolutePath() );

                    if ( asekShipList != null && asekShipList.size() > 0 )
                    {
                        //to get the ASEK SHip Details details
                        getASEKShipDetails( asekShipList );
                    }

                    successFileList.add( fileList[i] );
                }
                 success = true;
            }
            catch ( Exception e )
            {
                success = false;
                log( "Exception while parsing and storing file: " + fileList[i].getAbsolutePath() + "\nException: " + e );
                e.printStackTrace();
                throw e;
            }
        }

        /*Updating the Die Bank Lot Information*/
        try
        {
            // Save Die Bank Information
            if (m_dieBankDataMap.isEmpty() == false)
            {
                log(" Updating Die Bank Lot Information");
                InventoryLotDAO.saveInventoryLot(null, m_dieBankDataMap.values().iterator(), LOCATION_TYPE_DIE_BANK, VENDOR_NAME_ASEK);
            }
            else
            {
                InventoryLotDAO.closeInventoryLot(null, LOCATION_TYPE_DIE_BANK, VENDOR_NAME_ASEK);
            }
            success = true;
            m_dieBankDataMap.clear();
            m_dieBankDataMap = null;
        }
        catch (ScalarException se)
        {
            success = false;
            se.printStackTrace();
            log("ERROR!! ERROR!! ERROR!! Could not update Die Bank Lot information into database: " + se);
            throw se;
        }
        /*Assembly Lot Saving*/
       try
        {
            // Save Assembly Information
            if ( m_assemblyDataMap.isEmpty() == false )
            {
                log( " Updating Assembly Lot Information" );
                AssemblyAdaptorDAO.saveAssemblyLot( null, m_assemblyDataMap.values().iterator(),WIP_TYPE_ASSEMBLY, VENDOR_NAME_ASEK );
            }
            else
            {
                AssemblyAdaptorDAO.closeAssemblyLot( null, WIP_TYPE_ASSEMBLY, VENDOR_NAME_ASEK);
            }
            success = true;
            m_assemblyDataMap.clear();
            m_assemblyDataMap = null;
        }
        catch (ScalarException se)
        {
            success = false;
            se.printStackTrace();
            log( "ERROR!! ERROR!! ERROR!! Could not update Assembly Lot information into database: " + se );
            throw se;
        }
        // Save Assembly Ship Information
        try
        {
            if ( m_assyShipDataMap.isEmpty() == false )
            {
                log( " Updating Assembly Shiping Lot Information" );
                ShippedLotDAO.saveShippedLot( null, m_assyShipDataMap.values().iterator() );
            }
            success = true;
            m_assyShipDataMap.clear();
            m_assyShipDataMap = null;
        }
        catch (ScalarException se)
        {
            success = false;
            se.printStackTrace();
            log( "ERROR!! ERROR!! ERROR!! Could not update Assembly Shipping Lot information into database: " + se );
            throw se;
        }

        // To Close the Drop Out Lots
        AdaptorDAO.closeDropOutLots(VENDOR_NAME_ASEK);

        // TO SEND THE NEW LOTS, NEW TRANSIT LOTS, NO PO LOTS, DISCREPANCY LOTS NOTIFICATIONS TO THE USER
        AdaptorDAO.sendAllNotifications(VENDOR_NAME_ASEK, true, true, true);


       int adaptor_id = AdaptorRunInfo.getLastRunAdaptorId( ASEKPOP3Adaptor.ADAPTOR_NAME, CONSTANT_ADAPTOR);

        // Update Adaptor_Run_Info table
        try
        {
            if ( success == true )
            {
                AdaptorRunInfo.saveAdaptorLastUpdatedStatus(CONSTANT_PARSER, ASEKPOP3Adaptor.ADAPTOR_NAME, CONSTANT_SUCCESS, adaptor_id);
                System.out.println( "SUCCESSFULLY UPDATED IN THE DATABASE" );
            }
        }
        catch ( ScalarException se )
        {
            success = false; //As filES DATA SAVED , UPDATEING AS SUCCESS
            AdaptorRunInfo.saveAdaptorLastUpdatedStatus(CONSTANT_PARSER, ASEKPOP3Adaptor.ADAPTOR_NAME, CONSTANT_SUCCESS, adaptor_id);
            log( "WARNING!! WARNING!! WARNING!! Could not update Adaptor run information, Exception: " + se );
            throw se;
        }
        // Move the successful files to 'done' directory
       if ( success )
       {
           AdaptorUtils.moveFilesToDone( successFileList, doneDestDir );
       }
    }

    /**
     *
     * @param fileName
     * @return
     * @throws Exception
     */
    private ArrayList parseASEKWipReport( String fileName ) throws Exception
    {
        if ( fileName == null ) throw new ScalarException( "Filename argument is null" );

        ArrayList wipList              = new ArrayList();
        String line                     = null;

        try
        {
            File detailWipFile = new File( fileName );
            if ( detailWipFile.exists() == false )
            {
                throw new ScalarException( fileName + " doesnt exist" );
            }

            log( "\nFound " + fileName );
            log( "\nParsing the file for ASEK Wip Report..." );
            System.out.println( "\nFound " + fileName );
            System.out.println( "\nParsing the file for ASEK Wip Report..." );

            BufferedReader reader = new BufferedReader( new FileReader( detailWipFile ) );
            reader.readLine();  // Read out <Header Info > Line
            reader.readLine();
            reader.readLine();
            reader.readLine();

            while ( ( line = reader.readLine() ) != null  )
            {
                ASEKWipStatus wipStatus = new ASEKWipStatus();

                line = line.replaceAll( "\"", "" );
                String[] token = line.split( "," );

                for (int x = 0; x < token.length; x++)
                {
                    if (token[x] != null && token[x].trim().length() > 0   )
                    {
                        switch (x)
                        {
                            case 0:
                                    wipStatus.setReleaseNo( token[x].trim() );
                                    break;
                            case 1:
									wipStatus.setIssueDate( "99999999".equalsIgnoreCase(token[x].trim() ) ? null : token[x].trim() );
                                    break;
                            case 2:
                                    if ( !token[x].startsWith("====") && !token[x].startsWith("__") && !token[x].startsWith("2ND/OP") )
                                    {
                                        wipStatus.setPkgType( token[x].trim().split(" ")[0] );
                                        wipStatus.setLeadCount( token[x].trim().split(" ")[1] );
                                    }
                                    break;
                            case 3:
                                    wipStatus.setDeviceName( token[x].trim() );
                                    break;
                            //case 4: //P Not Requored
                            case 5:
                                    wipStatus.setFabLotNumber( token[x].trim());
                                    break;
                            case 6:
                                    wipStatus.setVendorLotNo( token[x].trim() );
                                    break;
                            case 7:
                                    wipStatus.setUnIssueQty( "0".equalsIgnoreCase(token[x].trim() ) ? null : token[x].trim() );
                                    break;
                            case 8:
                                    wipStatus.setProbeQty( "0".equalsIgnoreCase(token[x].trim() ) ? null : token[x].trim() );
                                    break;
                            case 9:
                                    wipStatus.setSortQty ( "0".equalsIgnoreCase(token[x].trim() ) ? null : token[x].trim() );
                                    break;
                            case 10:
                                    wipStatus.setGrind( "0".equalsIgnoreCase(token[x].trim() ) ? null : token[x].trim()  );
                                    break;
                            case 11:
                                    wipStatus.setWireBond ("0".equalsIgnoreCase(token[x].trim() ) ? null : token[x].trim() );
                                    break;
                            case 12:
                                    wipStatus.setDieAttach( "0".equalsIgnoreCase(token[x].trim() ) ? null : token[x].trim()  );
                                    break;
                            case 13:
                                    wipStatus.setEpCure( "0".equalsIgnoreCase(token[x].trim() ) ? null : token[x].trim() );
                                    break;
                            case 14:
                                    wipStatus.setOptical3rdInspection( "0".equalsIgnoreCase(token[x].trim() ) ? null : token[x].trim() );
                                    break;
                            case 15:
                                    wipStatus.setDieCoat( "0".equalsIgnoreCase(token[x].trim() ) ? null : token[x].trim() );
                                    break;
                            case 16:
                                    wipStatus.setPreMoldBake( "0".equalsIgnoreCase(token[x].trim() ) ? null : token[x].trim() );
                                    break;
                            case 17:
                                    wipStatus.setLaserMarking( "0".equalsIgnoreCase(token[x].trim() ) ? null : token[x].trim() );
                                    break;
                            case 18:
                                    wipStatus.setMarking( "0".equalsIgnoreCase(token[x].trim() ) ? null : token[x].trim() );
                                    break;
                            case 19:
                                    wipStatus.setDejunkTrim( "0".equalsIgnoreCase(token[x].trim() ) ? null : token[x].trim() );
                                    break;
                            case 20:
                                    wipStatus.setPreMoldCure("0".equalsIgnoreCase(token[x].trim() ) ? null : token[x].trim() );
                                    break;
                            case 21:
                                    wipStatus.setPostMoldCure("0".equalsIgnoreCase(token[x].trim() ) ? null : token[x].trim() );
                                    break;
                            case 22:
                                    wipStatus.setTempCycle("0".equalsIgnoreCase(token[x].trim() ) ? null : token[x].trim() );
                                    break;
                            case 23:
                                    wipStatus.setElectricDeflash("0".equalsIgnoreCase(token[x].trim() ) ? null : token[x].trim() );
                                    break;
                            case 24:
                                    wipStatus.setSolderPlating("0".equalsIgnoreCase(token[x].trim() ) ? null : token[x].trim() );
                                    break;
                            case 25:
                                    wipStatus.setForming("0".equalsIgnoreCase(token[x].trim() ) ? null : token[x].trim() );
                                    break;
                            case 26:
                                    wipStatus.setTopMark("0".equalsIgnoreCase(token[x].trim() ) ? null : token[x].trim() );
                                    break;
                            case 27:
                                    wipStatus.setFinalInspection("0".equalsIgnoreCase(token[x].trim() ) ? null : token[x].trim() );
                                    break;
                            case 28:
                                    wipStatus.setSingulation("0".equalsIgnoreCase(token[x].trim() ) ? null : token[x].trim() );
                                    break;
                            case 29:
                                    wipStatus.setOpenShort("0".equalsIgnoreCase(token[x].trim() ) ? null : token[x].trim() );
                                    break;
                            case 30:
                                    wipStatus.setFinalQaGate("0".equalsIgnoreCase(token[x].trim() ) ? null : token[x].trim());
                                    break;
                            case 31:
                                    wipStatus.setCommitDate( token[x].trim() );
                                    break;
                            case 32:
                                    wipStatus.setRevisedSOD(token[x].trim());
                                    break;
                            case 33:
                                    wipStatus.setLotType(token[x].trim());
                                    break;
                            case 34:
                                    wipStatus.setWaferLotNo(token[x].trim());
                                    break;
                            }
                      }
                }
                if ( wipStatus.getDeviceName() != null && wipStatus.getDeviceName().length() >0
                        && !wipStatus.getDeviceName().startsWith("==") && !wipStatus.getDeviceName().startsWith("____")&& !wipStatus.getDeviceName().startsWith("DEVICE")
                        && wipStatus.getFabLotNumber() != null && wipStatus.getFabLotNumber().length() >0 )
                {
                    wipList.add( wipStatus );
                }
            }

            try
            {
                if ( reader != null )
                {
                   reader.close();
                }
            }
            catch(IOException ignore)
            {
            }
        }
        catch ( Exception ex )
        {
            System.out.println("ERROR!!!ERROR!!! While Parsing the ASEK Wip Report");
            ex.printStackTrace();
            throw ex;
        }

        return wipList;

    } // end of prase Assembly Wip Report method

    /**
     *
     * @param fileName
     * @return
     * @throws Exception
     */
    private ArrayList parseASEKShipReport( String fileName ) throws Exception
    {
        if ( fileName == null ) throw new ScalarException( "Filename argument is null" );

        ArrayList shipList              = new ArrayList();
        ASEShipStatus  shipStatus       = null;
        try
        {
            File detailWipFile = new File( fileName );
            if ( detailWipFile.exists() == false )
            {
                throw new ScalarException( fileName + " doesnt exist" );
            }

            log( "\nFound " + fileName );
            log( "\nParsing the file for ASEK Ship Info..." );
            System.out.println( "\nFound " + fileName );
            System.out.println( "\nParsing the file for ASEK Ship Info..." );

            Workbook workbook = Workbook.getWorkbook( detailWipFile );
            Sheet sheet = workbook.getSheet( 0 );
            Cell[] row = null;

            for ( int rowCount = 2; rowCount < sheet.getRows(); rowCount++ )
            {
                row = sheet.getRow( rowCount );

                shipStatus = new ASEShipStatus();

                //row[0] not Required
                //row[1] not Required

                if( row.length > 2)
                {
                    if ( row[2].getType().equals(CellType.EMPTY) )
                    {
                        shipStatus.setInvoiceNo( null );
                    }
                    else
                    {
                        shipStatus.setInvoiceNo( row[2].getContents().trim() );
                    }
                }
                if( row.length > 3)
                {
                    if ( row[3].getType().equals(CellType.EMPTY) )
                    {
                        shipStatus.setPoNumber( null );
                    }
                    else
                    {
                        shipStatus.setPoNumber( row[3].getContents().trim() );
                    }
                }
                if( row.length > 4)
                {
                    if ( row[4].getType().equals(CellType.EMPTY) )
                    {
                        shipStatus.setFabLotNo( null );
                    }
                    else
                    {
                        shipStatus.setFabLotNo( row[4].getContents().trim());
                    }
                }
                if ( row.length > 5 )
                {
                   if ( row[5].getType().equals(CellType.EMPTY) )
                    {
                        shipStatus.setLotNumber( null );
                    }
                    else
                    {
                        shipStatus.setLotNumber( row[5].getContents().trim());
                    }
                }

                if( row.length > 6 )
                {
                    if ( row[6].getType().equals(CellType.EMPTY) )
                    {
                        shipStatus.setDeviceName( null );
                    }
                    else
                    {
                        shipStatus.setDeviceName( row[6].getContents().trim());
                    }
                }
                //ROW 7 MATERIAL not required
                //ROW 8 WAFER_PIECES not required
                if( row.length >9 )
                {
                    if ( row[9].getType().equals(CellType.EMPTY) )
                    {
                        shipStatus.setShipDieQty( null );
                    }
                    else
                    {
                        shipStatus.setShipDieQty( row[9].getContents().trim() );
                    }
                }
                // ROW 10 BINCODE not required
                if ( row.length > 11 )
                {
                    if ( row[11].getType().equals(CellType.EMPTY) )
                    {
                        shipStatus.setShipDate( null );
                    }
                    else
                    {
                        shipStatus.setShipDate( row[11].getContents().trim() );
                    }
                }
                if( row.length > 12 )
                {
                    if ( row[12].getType().equals(CellType.EMPTY) )
                    {
                        shipStatus.setNextLocation( null );
                    }
                    else
                    {
                        shipStatus.setNextLocation( row[12].getContents().trim());
                    }
                }
                // ROW 13 DROP_FLAG not required
                if( row.length > 14 )
                {
                    if ( row[14].getType().equals(CellType.EMPTY) )
                    {
                        shipStatus.setFlightNo( null );
                    }
                    else
                    {
                        shipStatus.setFlightNo( row[14].getContents().trim());
                    }
                }
                if( row.length >15 )
                {
                    if ( row[15].getType().equals(CellType.EMPTY) )
                    {
                        shipStatus.setMawb( null );
                    }
                    else
                    {
                        shipStatus.setMawb( row[15].getContents().trim());
                    }
                }
                if( row.length >16 )
                {
                    if ( row[16].getType().equals(CellType.EMPTY) )
                    {
                        shipStatus.setHawb( null );
                    }
                    else
                    {
                        shipStatus.setHawb( row[16].getContents().trim());
                    }
                }
                if( row.length >17 )
                {
                    if ( row[17].getType().equals(CellType.EMPTY) )
                    {
                        shipStatus.setEtd( null );
                    }
                    else
                    {
                        shipStatus.setEtd( row[17].getContents().trim());
                    }
                }
                if( row.length >18 )
                {
                    if ( row[18].getType().equals(CellType.EMPTY) )
                    {
                        shipStatus.setEta( null );
                    }
                    else
                    {
                        shipStatus.setEta( row[18].getContents().trim());
                    }
                }
                if( row.length >19 )
                {
                    if ( row[19].getType().equals(CellType.EMPTY) )
                    {
                        shipStatus.setLotType( null );
                    }
                    else
                    {
                        shipStatus.setLotType( row[19].getContents().trim());
                    }
                }

                if ( shipStatus.getDeviceName() != null && shipStatus.getDeviceName().length() >0
                     && shipStatus.getFabLotNo() != null && shipStatus.getFabLotNo().length() >0 )
                {
                    shipList.add( shipStatus );
                }
            }
        }
        catch ( Exception ex )
        {
            System.out.println("ERROR!!!ERROR!!! While Parsing the ASEK SHIP sheet");
            ex.printStackTrace();
            throw ex;
        }
        return shipList;
    }// end of prase Shipment Report method

    /**
     *
     * @param lotstatusList
     * @throws Exception
     */
    private void getASEKAssyWipDetails( ArrayList lotstatusList ) throws Exception
    {
        ASEKWipStatus wipStatus = null;
        AssemblyLot assemblyLot = null;
        InventoryLot dbInventoryLot = null;
        String lotNumber        = null;
        String partNumber       = null;
        String fablotNumber     = null;

        if (lotstatusList == null)
        {
            return;
        }

        Iterator iterator = lotstatusList.iterator();
        try
        {
            while (iterator.hasNext())
            {
                wipStatus = (ASEKWipStatus) iterator.next();

                fablotNumber    = wipStatus.getFabLotNumber();
                partNumber      = wipStatus.getDeviceName();
                lotNumber       = wipStatus.getVendorLotNo();

                if( lotNumber == null )
                {
                    lotNumber = fablotNumber;
                }

                if ( lotNumber != null && lotNumber.trim().length() > 0 && partNumber != null && partNumber.trim().length() > 0)
                {
                    //DIE BANK
                    if ( wipStatus.getUnIssueQty() != null && wipStatus.getVendorLotNo() == null )
                    {
                        /*int bomPocessstep_id = NewAdaptorDAO.getBOMProcessStepId( partNumber , WIP_TYPE_ASSEMBLY );

                        if ( bomPocessstep_id > 0 )
                        {
                            log ( "In BOM Die bank--> Assembly Bom " + lotNumber );
                            assemblyLot = (AssemblyLot) m_assemblyDataMap.get( partNumber + lotNumber + fablotNumber );
                            if (assemblyLot == null)
                            {
                                assemblyLot = new AssemblyLot( VENDOR_NAME_ASEK,  WIP_TYPE_ASSEMBLY );
                                m_assemblyDataMap.put( partNumber + lotNumber + fablotNumber, assemblyLot);
                            }
                            int quantity = 0;
                            if (assemblyLot.getQtyIn() != null)
                            {
                                try
                                {
                                    quantity = Integer.parseInt(assemblyLot.getQtyIn());
                                }
                                catch (NumberFormatException ignore) {
                                }
                                try
                                {
                                    quantity += Integer.parseInt(wipStatus.getUnIssueQty());
                                }
                                catch (NumberFormatException nfe) {
                                }
                            }
                            else
                            {
                                try
                                {
                                    quantity = Integer.parseInt(wipStatus.getUnIssueQty());
                                }
                                catch (NumberFormatException ignore) {
                                }
                            }
                            assemblyLot.setLotNumber( lotNumber );
                            assemblyLot.setDeviceName( partNumber );
                            assemblyLot.setStatus(LOT_STATUS_RUNNING);
                            assemblyLot.setFabLotNumber( fablotNumber );
                            assemblyLot.setPackageType( wipStatus.getPkgType() );
                            assemblyLot.setCategory( LOT_CATEGORY_TYPE_PROD );
                            assemblyLot.setLeadCount( wipStatus.getLeadCount() );
                            assemblyLot.setQtyIn( quantity+"");
                            if ( wipStatus.getIssueDate() != null )
                            {
                                assemblyLot.setDateIn( StringUtils.formatDate(wipStatus.getIssueDate(), "yyyyMMdd","MM/dd/yyyy" ) );
                            }
                            else
                            {
                                assemblyLot.setDateIn( DateUtil.formatDate(new Date(), "MM/dd/yyyy") );
                            }
                        }
                        else
                        {*/
                            log( lotNumber +" In DieBank");

                            if ( partNumber.endsWith("-00") )
                            {
                                partNumber = partNumber.substring(0, partNumber.lastIndexOf("-00") );
                            }
                            dbInventoryLot = (InventoryLot)m_dieBankDataMap.get( partNumber + fablotNumber );

                            if ( dbInventoryLot == null )
                            {
                                dbInventoryLot = new InventoryLot( VENDOR_NAME_ASEK, LOCATION_TYPE_DIE_BANK );
                                m_dieBankDataMap.put( partNumber + fablotNumber, dbInventoryLot );
                            }

                            int quantity = 0;
                            if (dbInventoryLot.getQtyIn() != null)
                            {
                                try
                                {
                                    quantity = Integer.parseInt(dbInventoryLot.getQtyIn());
                                }
                                catch (NumberFormatException ignore) {
                                }
                                try
                                {
                                    quantity += Integer.parseInt(wipStatus.getUnIssueQty());
                                }
                                catch (NumberFormatException nfe) {
                                }
                            }
                            else
                            {
                                try
                                {
                                    quantity = Integer.parseInt( wipStatus.getUnIssueQty() );
                                }
                                catch (NumberFormatException ignore) {
                                }
                            }
                            if (quantity > 0 )
                            {
                                dbInventoryLot.setPartNumber( partNumber );
                                dbInventoryLot.setLotNumber( fablotNumber );
                                dbInventoryLot.setQtyIn(quantity + "");
                                if ( wipStatus.getIssueDate() != null )
                                {
                                    dbInventoryLot.setDateIn( StringUtils.formatDate(wipStatus.getIssueDate(), "yyyyMMdd","MM/dd/yyyy" ) );
                                }
                                
                                dbInventoryLot.setFabLotNumber( fablotNumber );
                                dbInventoryLot.setPkgType( wipStatus.getPkgType() );
                                dbInventoryLot.setLeadCount( wipStatus.getLeadCount() );
                                dbInventoryLot.setStatus( LOT_STATUS_RUNNING );
                                dbInventoryLot.setLotCategory( LOT_CATEGORY_TYPE_PROD );
                            }
                        //}
                    }

                     //ASSEMBLY
                   if (  wipStatus.getVendorLotNo() != null && ( wipStatus.getUnIssueQty() != null || wipStatus.getGrind() != null || wipStatus.getWireBond() != null || wipStatus.getDieAttach() != null || wipStatus.getEpCure() != null
                            || wipStatus.getOptical3rdInspection() != null || wipStatus.getDieCoat() != null || wipStatus.getPreMoldBake() != null || wipStatus.getLaserMarking() != null
                            || wipStatus.getMarking() != null || wipStatus.getDejunkTrim() != null || wipStatus.getPreMoldCure() != null || wipStatus.getPostMoldCure() != null
                            || wipStatus.getTempCycle() != null || wipStatus.getElectricDeflash() != null || wipStatus.getSolderPlating() != null || wipStatus.getForming() != null
                            || wipStatus.getTopMark() != null || wipStatus.getFinalInspection() != null || wipStatus.getSingulation() != null || wipStatus.getOpenShort() != null
                            || wipStatus.getFinalQaGate() != null )  )
                   {
                        log( "In Assembly" + lotNumber ) ;

                        assemblyLot = (AssemblyLot) m_assemblyDataMap.get( partNumber + lotNumber + fablotNumber );
                        if (assemblyLot == null)
                        {
                            assemblyLot = new AssemblyLot(VENDOR_NAME_ASEK, WIP_TYPE_ASSEMBLY);
                            m_assemblyDataMap.put( partNumber + lotNumber + fablotNumber, assemblyLot);
                        }

                        // FOR SETTING THE QTY IN
                       if( wipStatus.getUnIssueQty() != null )
                        {
                            assemblyLot = (AssemblyLot) m_assemblyDataMap.get(partNumber + lotNumber + fablotNumber);
                            if( assemblyLot.getQtyIn() != null )
                            {
                                try
                                {
                                    assemblyLot.setQtyIn( ( Integer.parseInt( assemblyLot.getQtyIn() ) + Integer.parseInt( wipStatus.getUnIssueQty() ) ) + "" );
                                }
                                catch(Exception e)
                                {
                                    assemblyLot.setQtyIn( wipStatus.getUnIssueQty() );
                                }
                            }
                            else
                            {
                                assemblyLot.setQtyIn( wipStatus.getUnIssueQty() );
                            }
                            if ( assemblyLot.getStage() != null )
                            {
                                assemblyLot.setStage ( assemblyLot.getStage() +", Un Issue") ;
                            }
                            else
                            {
                                assemblyLot.setStage("Un Issue");
                            }
                        }
                        if( wipStatus.getGrind() != null )
                        {
                            assemblyLot = (AssemblyLot) m_assemblyDataMap.get(partNumber + lotNumber + fablotNumber);
                            if( assemblyLot.getQtyIn() != null )
                            {
                                try
                                {
                                    assemblyLot.setQtyIn( ( Integer.parseInt( assemblyLot.getQtyIn() ) + Integer.parseInt( wipStatus.getGrind() ) ) + "" );
                                }
                                catch(Exception e)
                                {
                                    assemblyLot.setQtyIn( wipStatus.getGrind() );
                                }
                            }
                            else
                            {
                                assemblyLot.setQtyIn( wipStatus.getGrind() );
                            }
                            if ( assemblyLot.getStage() != null )
                            {
                                assemblyLot.setStage ( assemblyLot.getStage() +", Grinding") ;
                            }
                            else
                            {
                                assemblyLot.setStage("Grinding");
                            }
                        }
                        if( wipStatus.getWireBond() != null  )
                        {
                           assemblyLot = (AssemblyLot) m_assemblyDataMap.get(partNumber + lotNumber + fablotNumber);
                           if( assemblyLot.getQtyIn() != null )
                           {
                                try
                                {
                                    assemblyLot.setQtyIn( ( Integer.parseInt( assemblyLot.getQtyIn() ) + Integer.parseInt( wipStatus.getWireBond() ) ) + "" );
                                }
                                catch(Exception e)
                                {
                                    assemblyLot.setQtyIn( wipStatus.getWireBond() );
                                }
                            }
                            else
                            {
                                assemblyLot.setQtyIn( wipStatus.getWireBond() );
                            }
                            if ( assemblyLot.getStage() != null )
                            {
                                assemblyLot.setStage ( assemblyLot.getStage() +", W/M,W/S,2ND/OP,QA/GATE") ;
                            }
                            else
                            {
                                assemblyLot.setStage("W/M,W/S,2ND/OP,QA/GATE");
                            }
                        }

                        if( wipStatus.getDieAttach() != null )
                        {
                           assemblyLot = (AssemblyLot) m_assemblyDataMap.get(partNumber + lotNumber + fablotNumber);
                           if( assemblyLot.getQtyIn() != null )
                           {
                                try
                                {
                                    assemblyLot.setQtyIn( ( Integer.parseInt( assemblyLot.getQtyIn() ) + Integer.parseInt( wipStatus.getDieAttach() ) ) + "" );
                                }
                                catch(Exception e)
                                {
                                    assemblyLot.setQtyIn( wipStatus.getDieAttach() );
                                }
                            }
                            else
                            {
                                assemblyLot.setQtyIn( wipStatus.getDieAttach() );
                            }
                            if ( assemblyLot.getStage() != null )
                            {
                                assemblyLot.setStage ( assemblyLot.getStage() +", Die Attach") ;
                            }
                            else
                            {
                                assemblyLot.setStage("Die Attach");
                            }
                        }

                        if( wipStatus.getEpCure() != null )
                        {
                           assemblyLot = (AssemblyLot) m_assemblyDataMap.get(partNumber + lotNumber + fablotNumber);

                           if( assemblyLot.getQtyIn() != null )
                           {
                                try
                                {
                                    assemblyLot.setQtyIn( ( Integer.parseInt( assemblyLot.getQtyIn() ) + Integer.parseInt( wipStatus.getEpCure() ) ) + "" );
                                }
                                catch(Exception e)
                                {
                                    assemblyLot.setQtyIn( wipStatus.getEpCure() );
                                }
                            }
                            else
                            {
                                assemblyLot.setQtyIn( wipStatus.getEpCure() );
                            }
                            if ( assemblyLot.getStage() != null )
                            {
                                assemblyLot.setStage( assemblyLot.getStage() +", EPOXY Cure,Wire Bonder") ;
                            }
                            else
                            {
                                assemblyLot.setStage("EPOXY Cure,Wire Bonder");
                            }
                        }

                        if( wipStatus.getOptical3rdInspection() != null  )
                        {
                           assemblyLot = (AssemblyLot) m_assemblyDataMap.get(partNumber + lotNumber + fablotNumber);
                           if( assemblyLot.getQtyIn() != null )
                           {
                                try
                                {
                                    assemblyLot.setQtyIn( ( Integer.parseInt( assemblyLot.getQtyIn() ) + Integer.parseInt( wipStatus.getOptical3rdInspection() ) ) + "" );
                                }
                                catch(Exception e)
                                {
                                    assemblyLot.setQtyIn( wipStatus.getOptical3rdInspection() );
                                }
                            }
                            else
                            {
                                assemblyLot.setQtyIn( wipStatus.getOptical3rdInspection() );
                            }
                            if ( assemblyLot.getStage() != null )
                            {
                                assemblyLot.setStage ( assemblyLot.getStage() +", 3rdOptical") ;
                            }
                            else
                            {
                                assemblyLot.setStage("3rdOptical");
                            }
                        }

                        if( wipStatus.getDieCoat() != null  )
                        {
                            assemblyLot = (AssemblyLot) m_assemblyDataMap.get(partNumber + lotNumber + fablotNumber);
                           if( assemblyLot.getQtyIn() != null )
                           {
                                try
                                {
                                    assemblyLot.setQtyIn( ( Integer.parseInt( assemblyLot.getQtyIn() ) + Integer.parseInt( wipStatus.getDieCoat() ) ) + "" );
                                }
                                catch(Exception e)
                                {
                                    assemblyLot.setQtyIn( wipStatus.getDieCoat() );
                                }
                            }
                            else
                            {
                                assemblyLot.setQtyIn( wipStatus.getDieCoat() );
                            }
                            if ( assemblyLot.getStage() != null )
                            {
                                assemblyLot.setStage ( assemblyLot.getStage() +", DieCoat") ;
                            }
                            else
                            {
                                assemblyLot.setStage("DieCoat");
                            }
                        }

                        if( wipStatus.getPreMoldBake() != null  )
                        {
                            assemblyLot = (AssemblyLot) m_assemblyDataMap.get(partNumber + lotNumber + fablotNumber);
                           if( assemblyLot.getQtyIn() != null )
                           {
                                try
                                {
                                    assemblyLot.setQtyIn( ( Integer.parseInt( assemblyLot.getQtyIn() ) + Integer.parseInt( wipStatus.getPreMoldBake() ) ) + "" );
                                }
                                catch(Exception e)
                                {
                                    assemblyLot.setQtyIn( wipStatus.getPreMoldBake() );
                                }
                            }
                            else
                            {
                                assemblyLot.setQtyIn( wipStatus.getPreMoldBake() );
                            }
                            if ( assemblyLot.getStage() != null )
                            {
                                assemblyLot.setStage ( assemblyLot.getStage() +", PreMoldBake") ;
                            }
                            else
                            {
                                assemblyLot.setStage("PreMoldBake");
                            }
                        }

                        if( wipStatus.getLaserMarking() != null  )
                        {
                            assemblyLot = (AssemblyLot) m_assemblyDataMap.get(partNumber + lotNumber + fablotNumber);
                           if( assemblyLot.getQtyIn() != null )
                           {
                                try
                                {
                                    assemblyLot.setQtyIn( ( Integer.parseInt( assemblyLot.getQtyIn() ) + Integer.parseInt( wipStatus.getLaserMarking() ) ) + "" );
                                }
                                catch(Exception e)
                                {
                                    assemblyLot.setQtyIn( wipStatus.getLaserMarking() );
                                }
                            }
                            else
                            {
                                assemblyLot.setQtyIn( wipStatus.getLaserMarking() );
                            }
                            if ( assemblyLot.getStage() != null )
                            {
                                assemblyLot.setStage ( assemblyLot.getStage() +", Laser Marking") ;
                            }
                            else
                            {
                                assemblyLot.setStage("Laser Marking");
                            }
                        }

                        if( wipStatus.getMarking() != null )
                        {
                            assemblyLot = (AssemblyLot) m_assemblyDataMap.get(partNumber + lotNumber + fablotNumber);
                           if( assemblyLot.getQtyIn() != null )
                           {
                                try
                                {
                                    assemblyLot.setQtyIn( ( Integer.parseInt( assemblyLot.getQtyIn() ) + Integer.parseInt( wipStatus.getMarking() ) ) + "" );
                                }
                                catch(Exception e)
                                {
                                    assemblyLot.setQtyIn( wipStatus.getMarking() );
                                }
                            }
                            else
                            {
                                assemblyLot.setQtyIn( wipStatus.getMarking() );
                            }
                            if ( assemblyLot.getStage() != null )
                            {
                                assemblyLot.setStage ( assemblyLot.getStage() +", Marking") ;
                            }
                            else
                            {
                                assemblyLot.setStage("Marking");
                            }
                        }

                        if( wipStatus.getDejunkTrim() != null  )
                        {
                            assemblyLot = (AssemblyLot) m_assemblyDataMap.get(partNumber + lotNumber + fablotNumber);
                           if( assemblyLot.getQtyIn() != null )
                           {
                                try
                                {
                                    assemblyLot.setQtyIn( ( Integer.parseInt( assemblyLot.getQtyIn() ) + Integer.parseInt( wipStatus.getDejunkTrim() ) ) + "" );
                                }
                                catch(Exception e)
                                {
                                    assemblyLot.setQtyIn( wipStatus.getDejunkTrim() );
                                }
                            }
                            else
                            {
                                assemblyLot.setQtyIn( wipStatus.getDejunkTrim() );
                            }
                            if ( assemblyLot.getStage() != null )
                            {
                                assemblyLot.setStage ( assemblyLot.getStage() +", dejunkTrim") ;
                            }
                            else
                            {
                                assemblyLot.setStage("dejunkTrim");
                            }
                        }

                        if( wipStatus.getPreMoldCure() != null  )
                        {
                            assemblyLot = (AssemblyLot) m_assemblyDataMap.get(partNumber + lotNumber + fablotNumber);
                           if( assemblyLot.getQtyIn() != null )
                           {
                                try
                                {
                                    assemblyLot.setQtyIn( ( Integer.parseInt( assemblyLot.getQtyIn() ) + Integer.parseInt( wipStatus.getPreMoldCure() ) ) + "" );
                                }
                                catch(Exception e)
                                {
                                    assemblyLot.setQtyIn( wipStatus.getPreMoldCure() );
                                }
                            }
                            else
                            {
                                assemblyLot.setQtyIn( wipStatus.getPreMoldCure() );
                            }
                            if ( assemblyLot.getStage() != null )
                            {
                                assemblyLot.setStage ( assemblyLot.getStage() +", preMoldCure") ;
                            }
                            else
                            {
                                assemblyLot.setStage("preMoldCure");
                            }
                        }
                        if( wipStatus.getPostMoldCure() != null )
                        {
                           assemblyLot = (AssemblyLot) m_assemblyDataMap.get(partNumber + lotNumber + fablotNumber);
                           if( assemblyLot.getQtyIn() != null )
                           {
                                try
                                {
                                    assemblyLot.setQtyIn( ( Integer.parseInt( assemblyLot.getQtyIn() ) + Integer.parseInt( wipStatus.getPostMoldCure() ) ) + "" );
                                }
                                catch(Exception e)
                                {
                                    assemblyLot.setQtyIn( wipStatus.getPostMoldCure() );
                                }
                            }
                            else
                            {
                                assemblyLot.setQtyIn( wipStatus.getPostMoldCure() );
                            }
                            if ( assemblyLot.getStage() != null )
                            {
                                assemblyLot.setStage ( assemblyLot.getStage() +", postMoldCure") ;
                            }
                            else
                            {
                                assemblyLot.setStage("postMoldCure");
                            }
                        }

                        if( wipStatus.getTempCycle() != null  )
                        {
                           assemblyLot = (AssemblyLot) m_assemblyDataMap.get(partNumber + lotNumber + fablotNumber);
                           if( assemblyLot.getQtyIn() != null )
                           {
                                try
                                {
                                    assemblyLot.setQtyIn( ( Integer.parseInt( assemblyLot.getQtyIn() ) + Integer.parseInt( wipStatus.getTempCycle() ) ) + "" );
                                }
                                catch(Exception e)
                                {
                                    assemblyLot.setQtyIn( wipStatus.getTempCycle() );
                                }
                            }
                            else
                            {
                                assemblyLot.setQtyIn( wipStatus.getTempCycle() );
                            }
                            if ( assemblyLot.getStage() != null )
                            {
                                assemblyLot.setStage ( assemblyLot.getStage() +", TempCycle") ;
                            }
                            else
                            {
                                assemblyLot.setStage("TempCycle");
                            }
                        }

                        if( wipStatus.getElectricDeflash() != null )
                        {
                           assemblyLot = (AssemblyLot) m_assemblyDataMap.get(partNumber + lotNumber + fablotNumber);
                           if( assemblyLot.getQtyIn() != null )
                           {
                                try
                                {
                                    assemblyLot.setQtyIn( ( Integer.parseInt( assemblyLot.getQtyIn() ) + Integer.parseInt( wipStatus.getElectricDeflash() ) ) + "" );
                                }
                                catch(Exception e)
                                {
                                    assemblyLot.setQtyIn( wipStatus.getElectricDeflash() );
                                }
                            }
                            else
                            {
                                assemblyLot.setQtyIn( wipStatus.getElectricDeflash() );
                            }
                            if ( assemblyLot.getStage() != null )
                            {
                                assemblyLot.setStage ( assemblyLot.getStage() +", Elec.Deflash") ;
                            }
                            else
                            {
                                assemblyLot.setStage("Elec.Deflash");
                            }
                        }
                        if( wipStatus.getSolderPlating() != null  )
                        {
                           assemblyLot = (AssemblyLot) m_assemblyDataMap.get(partNumber + lotNumber + fablotNumber);
                           if( assemblyLot.getQtyIn() != null )
                           {
                                try
                                {
                                    assemblyLot.setQtyIn( ( Integer.parseInt( assemblyLot.getQtyIn() ) + Integer.parseInt( wipStatus.getSolderPlating() ) ) + "" );
                                }
                                catch(Exception e)
                                {
                                    assemblyLot.setQtyIn( wipStatus.getSolderPlating() );
                                }
                            }
                            else
                            {
                                assemblyLot.setQtyIn( wipStatus.getSolderPlating() );
                            }
                            if ( assemblyLot.getStage() != null )
                            {
                                assemblyLot.setStage ( assemblyLot.getStage() +", SolderPlate") ;
                            }
                            else
                            {
                                assemblyLot.setStage("SolderPlate");
                            }
                        }

                        if( wipStatus.getForming() != null )
                        {
                           assemblyLot = (AssemblyLot) m_assemblyDataMap.get(partNumber + lotNumber + fablotNumber);
                           if( assemblyLot.getQtyIn() != null )
                           {
                                try
                                {
                                    assemblyLot.setQtyIn( ( Integer.parseInt( assemblyLot.getQtyIn() ) + Integer.parseInt( wipStatus.getForming() ) ) + "" );
                                }
                                catch(Exception e)
                                {
                                    assemblyLot.setQtyIn( wipStatus.getForming() );
                                }
                            }
                            else
                            {
                                assemblyLot.setQtyIn( wipStatus.getForming() );
                            }
                            if ( assemblyLot.getStage() != null )
                            {
                                assemblyLot.setStage ( assemblyLot.getStage() +", Forming") ;
                            }
                            else
                            {
                                assemblyLot.setStage("Forming");
                            }
                        }
                        if( wipStatus.getTopMark() != null  )
                        {
                           assemblyLot = (AssemblyLot) m_assemblyDataMap.get(partNumber + lotNumber + fablotNumber);
                           if( assemblyLot.getQtyIn() != null )
                           {
                                try
                                {
                                    assemblyLot.setQtyIn( ( Integer.parseInt( assemblyLot.getQtyIn() ) + Integer.parseInt( wipStatus.getTopMark() ) ) + "" );
                                }
                                catch(Exception e)
                                {
                                    assemblyLot.setQtyIn( wipStatus.getTopMark() );
                                }
                            }
                            else
                            {
                                assemblyLot.setQtyIn( wipStatus.getTopMark() );
                            }
                            if ( assemblyLot.getStage() != null )
                            {
                                assemblyLot.setStage ( assemblyLot.getStage() +", TopMark") ;
                            }
                            else
                            {
                                assemblyLot.setStage("TopMark");
                            }
                        }
                        if( wipStatus.getFinalInspection() != null )
                        {
                           assemblyLot = (AssemblyLot) m_assemblyDataMap.get(partNumber + lotNumber + fablotNumber);
                           if( assemblyLot.getQtyIn() != null )
                           {
                                try
                                {
                                    assemblyLot.setQtyIn( ( Integer.parseInt( assemblyLot.getQtyIn() ) + Integer.parseInt( wipStatus.getFinalInspection() ) ) + "" );
                                }
                                catch(Exception e)
                                {
                                    assemblyLot.setQtyIn( wipStatus.getFinalInspection() );
                                }
                            }
                            else
                            {
                                assemblyLot.setQtyIn( wipStatus.getFinalInspection() );
                            }
                            if ( assemblyLot.getStage() != null )
                            {
                                assemblyLot.setStage ( assemblyLot.getStage() +", FinalInspec.") ;
                            }
                            else
                            {
                                assemblyLot.setStage("FinalInspec.");
                            }
                        }
                        if( wipStatus.getSingulation() != null  )
                        {
                           assemblyLot = (AssemblyLot) m_assemblyDataMap.get(partNumber + lotNumber + fablotNumber);
                           if( assemblyLot.getQtyIn() != null )
                           {
                                try
                                {
                                    assemblyLot.setQtyIn( ( Integer.parseInt( assemblyLot.getQtyIn() ) + Integer.parseInt( wipStatus.getSingulation() ) ) + "" );
                                }
                                catch(Exception e)
                                {
                                    assemblyLot.setQtyIn( wipStatus.getSingulation() );
                                }
                            }
                            else
                            {
                                assemblyLot.setQtyIn( wipStatus.getSingulation() );
                            }
                            if ( assemblyLot.getStage() != null )
                            {
                                assemblyLot.setStage ( assemblyLot.getStage() +", Singulation") ;
                            }
                            else
                            {
                                assemblyLot.setStage("Singulation");
                            }
                        }
                        if( wipStatus.getOpenShort() != null  )
                        {
                           assemblyLot = (AssemblyLot) m_assemblyDataMap.get(partNumber + lotNumber + fablotNumber);
                           if( assemblyLot.getQtyIn() != null )
                           {
                                try
                                {
                                    assemblyLot.setQtyIn( ( Integer.parseInt( assemblyLot.getQtyIn() ) + Integer.parseInt( wipStatus.getOpenShort() ) ) + "" );
                                }
                                catch(Exception e)
                                {
                                    assemblyLot.setQtyIn( wipStatus.getOpenShort() );
                                }
                            }
                            else
                            {
                                assemblyLot.setQtyIn( wipStatus.getOpenShort() );
                            }
                            if ( assemblyLot.getStage() != null )
                            {
                                assemblyLot.setStage ( assemblyLot.getStage() +", Open/Short") ;
                            }
                            else
                            {
                                assemblyLot.setStage("Open/Short");
                            }
                        }
                        if( wipStatus.getFinalQaGate() != null  )
                        {
                           assemblyLot = (AssemblyLot) m_assemblyDataMap.get(partNumber + lotNumber + fablotNumber);
                           if( assemblyLot.getQtyIn() != null )
                           {
                                try
                                {
                                    assemblyLot.setQtyIn( ( Integer.parseInt( assemblyLot.getQtyIn() ) + Integer.parseInt( wipStatus.getFinalQaGate() ) ) + "" );
                                }
                                catch(Exception e)
                                {
                                    assemblyLot.setQtyIn( wipStatus.getFinalQaGate() );
                                }
                            }
                            else
                            {
                                assemblyLot.setQtyIn( wipStatus.getFinalQaGate() );
                            }
                            if ( assemblyLot.getStage() != null )
                            {
                                assemblyLot.setStage ( assemblyLot.getStage() +", final QA Gate") ;
                            }
                            else
                            {
                                assemblyLot.setStage("final QA Gate");
                            }
                        }

                        if ( assemblyLot.getQtyIn() != null )
                        {
                            assemblyLot.setLotNumber( lotNumber );
                            assemblyLot.setDeviceName( partNumber );
                            assemblyLot.setStatus(LOT_STATUS_RUNNING);
                            assemblyLot.setFabLotNumber( fablotNumber );
                            assemblyLot.setPackageType( wipStatus.getPkgType() );
                            assemblyLot.setCategory( LOT_CATEGORY_TYPE_PROD );
                            assemblyLot.setLeadCount( wipStatus.getLeadCount() );
                            if ( wipStatus.getIssueDate() != null )
                            {
                                assemblyLot.setDateIn( StringUtils.formatDate(wipStatus.getIssueDate(), "yyyyMMdd", "MM/dd/yyyy" ) );
                            }

                            if (wipStatus.getCommitDate() != null )
                            {
                                assemblyLot.setExpectedDateOut( StringUtils.formatDate(wipStatus.getCommitDate(), "yyyyMMdd", "MM/dd/yyyy" ) );
                            }
                            else if ( wipStatus.getRevisedSOD() != null )
                            {
                                 assemblyLot.setExpectedDateOut( StringUtils.formatDate(wipStatus.getRevisedSOD(), "yyyyMMdd", "MM/dd/yyyy" ) );
                            }

                        }
                    }
                }
            }
        }
        catch( Exception e)
        {
            System.out.println(" !!ERROR !! ERROR Exception while get getting the ASEK Assembly Wip Details");
            e.printStackTrace();
            throw e;
        }
    }


    private void getASEKShipDetails(ArrayList shippingDataList) throws Exception
    {
       if ( shippingDataList == null || shippingDataList.size() == 0 )
       {
            return;
       }
       ASEShipStatus shipStatus  = null;
       String partNumber                = null;
       String lotNumber                 = null;
       ShippedLot shippedLot          = null;
       String fabLotNumber              = null;
       String nextLocation              = null;
       try
       {
           Iterator iterator = shippingDataList.iterator();

           while ( iterator.hasNext() )
           {
               shipStatus = (ASEShipStatus) iterator.next();

               lotNumber    = shipStatus.getLotNumber() ;
               partNumber   = shipStatus.getDeviceName();
               fabLotNumber = shipStatus.getFabLotNo();
               nextLocation = shipStatus.getNextLocation();
               String shipDate = null;


               if ( shipStatus.getShipDate() != null )
               {
                   shipDate = StringUtils.formatDate( shipStatus.getShipDate(), "yyyyMMdd", "MM/dd/yyyy");
               }

               if ( lotNumber != null && partNumber != null && fabLotNumber != null )
               {
                   //System.out.println("lotNumber.substring(lotNumber.length() -4 ) ; = " + lotNumber.substring(0, (lotNumber.length() - 4 ) ));
                   lotNumber = lotNumber.substring(0, (lotNumber.length() - 4 ) ) ;

                   shippedLot = ( ShippedLot ) m_assyShipDataMap.get ( partNumber + lotNumber + fabLotNumber + nextLocation + shipDate );

                   if ( shippedLot == null )
                   {
                        shippedLot = new ShippedLot(VENDOR_NAME_ASEK, LOCATION_TYPE_ASSEMBLY);
                        m_assyShipDataMap.put( partNumber + lotNumber + fabLotNumber + nextLocation + shipDate, shippedLot );
                   }

                   int quantity = 0;
                   if ( shippedLot.getShipQty() != null )
                   {
                       try
                       {
                           quantity = Integer.parseInt( shippedLot.getShipQty() );
                       }
                       catch ( NumberFormatException ignore )
                       {
                           quantity = 0;
                       }
                       try
                       {
                           quantity += Integer.parseInt( shipStatus.getShipDieQty() );
                       }
                       catch ( NumberFormatException ignore )
                       {
                           quantity = 0;
                       }
                   }
                   else
                   {
                       try
                       {
                           quantity = Integer.parseInt( shipStatus.getShipDieQty() );
                       }
                       catch ( NumberFormatException ignore )
                       {
                           quantity = 0;
                       }
                   }

                   if ( quantity > 0 )
                   {
                       shippedLot.setLotNumber( lotNumber );
                       shippedLot.setPartNumber( partNumber );
                       shippedLot.setShipDate( shipDate );
                       shippedLot.setShipQty( quantity + "" );
                       shippedLot.setNextLocation( nextLocation );
                       shippedLot.setInvoiceNumber( shipStatus.getInvoiceNo() );
                       shippedLot.setFabLotNumber( fabLotNumber );
                       shippedLot.setMawb( shipStatus.getMawb() );
                       shippedLot.setHawb( shipStatus.getHawb() );
                       shippedLot.setEtd( StringUtils.formatDate( !"00000000".equalsIgnoreCase(shipStatus.getEtd()) ? shipStatus.getEtd(): null, "yyyyMMdd", "MM/dd/yyyy") );
                       shippedLot.setEta( StringUtils.formatDate( !"00000000".equalsIgnoreCase(shipStatus.getEta()) ? shipStatus.getEta(): null, "yyyyMMdd", "MM/dd/yyyy") );
                       shippedLot.setFlightNumber( shipStatus.getFlightNo() );
                       shippedLot.setProcessType( LOCATION_TYPE_ASSEMBLY );
                   }
               }
           }
        }
        catch(Exception e )
        {
            e.printStackTrace();
            log("Exception while geting ASEK Shipping inforamtion" + e);
            throw  e;
        }
    }

    private void log( String message )
    {
        if ( message == null ) return;

        if ( m_parserLogger != null )
        {
            m_parserLogger.logDebug( message + LINE_SEPARATOR );
        }
        else
        {
            System.out.println( message );
        }
    } // end of log method
}
